package com.rpa.server.servicerpaserver.expose.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rpa.secure.servicerpasecure.common.result.Result;
import com.rpa.server.servicerpaserver.crawler.entity.Clazz;
import com.rpa.server.servicerpaserver.crawler.service.ClazzService;
import com.rpa.server.servicerpaserver.expose.dto.ClazzDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @className: ClazzController
 * @author: lzt
 * @date: 2025/2/11 11:21
 * @Version: 1.0
 * @description:
 */
@RestController
@RequestMapping("/api/clazz")
@Slf4j
@Tag(name = "本系统课程学生名单相关接口", description = "ClazzController")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;
    @PostMapping("/getClazzEnum")
    @Operation(summary = "根据课程获取对应班级枚举信息", description = "getClazzEnum")
    public Result<List<Map<String, Object>>> getClazzEnum(@RequestBody ClazzDto clazzDto){
        QueryWrapper<Clazz> clazzLambdaQueryWrapper = new QueryWrapper<>();
        clazzLambdaQueryWrapper.select("DISTINCT account_id as accountId,course_id as courseId,clazz_id as clazzId,faculty")
                .lambda()
                .eq(Clazz::getAccountId, clazzDto.getAccountId())
                .eq(Clazz::getCourseId, clazzDto.getCourseId())
                .isNotNull(Clazz::getFaculty) // 确保 faculty 不为 null
                .ne(Clazz::getFaculty, "");
        List<Map<String, Object>> list = clazzService.listMaps(clazzLambdaQueryWrapper);
        List<Map<String, Object>> uniqueList = list.stream()
                .collect(Collectors.collectingAndThen(
                        Collectors.toMap(
                                item -> item.get("accountId") + "_" + item.get("courseId") + "_" + item.get("clazzId"),
                                item -> item,
                                (existing, replacement) -> existing // 保留第一个出现的记录
                        ),
                        map -> new ArrayList<>(map.values())
                ));
        return Result.success(uniqueList);
    }
    @PostMapping("/getStudentEnum")
    @Operation(summary = "根据班级获取对应学生枚举信息", description = "getStudentEnum")
    public Result<List<Map<String, Object>>> getStudentEnum(@RequestBody ClazzDto clazzDto){
        if (Objects.isNull(clazzDto)){
            return Result.error("参数不能为空！");
        }
        QueryWrapper<Clazz> clazzLambdaQueryWrapper = new QueryWrapper<>();
        clazzLambdaQueryWrapper.select("DISTINCT account_id as accountId,course_id as courseId,clazz_id as clazzId,student_number as studentNumber,user_name as userName")
                .lambda()
                .eq(Clazz::getAccountId, clazzDto.getAccountId())
                .eq(Clazz::getCourseId, clazzDto.getCourseId())
                .eq(Clazz::getClazzId, clazzDto.getClazzId());
        List<Map<String, Object>> list = clazzService.listMaps(clazzLambdaQueryWrapper);
        return Result.success(list);
    }
}
